package com.example.qzzm;

import java.util.ArrayList;
import java.util.List;

import com.guide.viewpager.PagerAdapter;
import com.guide.viewpager.ViewPager;
import com.guide.viewpager.ViewPager.OnPageChangeListener;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.CycleInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

/**
 * 引导页面
 * 
 * @author Administrator
 *
 */
public class GuideActivity extends Activity implements OnPageChangeListener {

	private ImageView ivEarth, ivEarth1;// 地球
	private ImageView ivHouse, ivHouse1;// 房子
	private ImageView ivCloud, ivCloud1;// 云
	private ImageView ivInfo, ivInfo1;// 信息
	private ImageView ivBoom, ivBoom1;// 氣球

	private Button btnGoinButton;

	private ViewPager pager;

	private List<View> views;

	private MyPageAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		views = new ArrayList<View>();
		initView();
	}

	class MyPageAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return views.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			// TODO Auto-generated method stub
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
			return views.get(position);
		}

	}

	private void initView() {
		pager = (ViewPager) findViewById(R.id.vp_guide);
		btnGoinButton = (Button) findViewById(R.id.btn_goin);
		btnGoinButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(GuideActivity.this, MainActivity.class));
				finish();
			}
		});
		
		View guide1 = LayoutInflater.from(this).inflate(
				R.layout.activity_guide_1, null);
		views.add(guide1);

		ivEarth = (ImageView) guide1.findViewById(R.id.iv_earth);
		ivHouse = (ImageView) guide1.findViewById(R.id.iv_house);
		ivCloud = (ImageView) guide1.findViewById(R.id.iv_cloud);
		ivInfo = (ImageView) guide1.findViewById(R.id.iv_info);
		ivBoom = (ImageView) guide1.findViewById(R.id.iv_boom);

		View guide2 = LayoutInflater.from(this).inflate(
				R.layout.activity_guide_2, null);
		views.add(guide2);

		ivEarth1 = (ImageView) guide2.findViewById(R.id.iv_earth);
		ivHouse1 = (ImageView) guide2.findViewById(R.id.iv_house);
		ivCloud1 = (ImageView) guide2.findViewById(R.id.iv_cloud);
		ivInfo1 = (ImageView) guide2.findViewById(R.id.iv_info);
		ivBoom1 = (ImageView) guide2.findViewById(R.id.iv_boom);

		mAdapter = new MyPageAdapter();
		pager.setAdapter(mAdapter);
		pager.setOnPageChangeListener(this);

		animation(0);
	}

	private void animation(int position) {
		int pivot = Animation.RELATIVE_TO_SELF;

		if (position == views.size() - 1) {
			btnGoinButton.setVisibility(View.VISIBLE);
			AlphaAnimation animationGo = new AlphaAnimation(0.2f, 1.0f);
			animationGo.setDuration(3000);
			btnGoinButton.setAnimation(animationGo);
		} else {
			btnGoinButton.setVisibility(View.GONE);
		}

		switch (position) {
		case 0:
			RotateAnimation animation1 = new RotateAnimation(-120, 0, pivot,
					0.5f, pivot, 0.5f);
			animation1.setDuration(2000);
			ivEarth.startAnimation(animation1);

			RotateAnimation animation2 = new RotateAnimation(-120, 0, pivot,
					0.5f, pivot, 1f);
			animation2.setDuration(2000);
			ivHouse.startAnimation(animation2);

			TranslateAnimation animation3 = new TranslateAnimation(-200f, 0,
					-10f, 0);
			animation3.setDuration(2000);
			ivBoom.startAnimation(animation3);

			TranslateAnimation animation4 = new TranslateAnimation(200f, 0,
					10f, 0);
			animation4.setDuration(3000);
			ivCloud.startAnimation(animation4);

			AlphaAnimation animation5 = new AlphaAnimation(0.2f, 1.0f);
			animation5.setDuration(3000);
			ScaleAnimation animation6 = new ScaleAnimation(0.0f, 0.8f, 0.0f,
					0.8f, Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation6.setDuration(2000);
			AnimationSet set = new AnimationSet(false);
			set.addAnimation(animation5);
			set.addAnimation(animation6);
			set.setFillAfter(true);
			ivInfo.setAnimation(set);

			break;
		case 1:

			RotateAnimation animation11 = new RotateAnimation(-120, 0, pivot,
					0.5f, pivot, 0.5f);
			animation11.setDuration(2000);
			ivEarth1.startAnimation(animation11);

			RotateAnimation animation21 = new RotateAnimation(-120, 0, pivot,
					0.5f, pivot, 1f);
			animation21.setDuration(2000);
			ivHouse1.startAnimation(animation21);

			TranslateAnimation animation31 = new TranslateAnimation(-500f, 0,
					-10f, 0);
			animation31.setDuration(2000);
			ivBoom1.startAnimation(animation31);

			TranslateAnimation animation41 = new TranslateAnimation(200f, 0,
					10f, 0);
			animation41.setDuration(3000);
			ivCloud1.startAnimation(animation41);

			AlphaAnimation animation51 = new AlphaAnimation(0.2f, 1.0f);
			animation51.setDuration(2000);
			ScaleAnimation animation61 = new ScaleAnimation(0.0f, 0.8f, 0.0f,
					0.8f, Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
			animation61.setDuration(1000);
			AnimationSet set1 = new AnimationSet(false);
			set1.addAnimation(animation51);
			set1.addAnimation(animation61);
			set1.setFillAfter(true);
			ivInfo1.setAnimation(set1);
			break;

		}
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		animation(position);// 开始动画
		System.out.println(position);
	}

	@Override
	public void onPageScrollStateChanged(int state) {

	}

}
